package com.youmingxing.data;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * Created by chihiro on 15/9/2.
 * 修改值时直接使用gdm.xx = xx
 * gdm.save()
 */

public class GlobalDataManager {
    //需要保存在sharedPreference的数据
    public static int userId;//我的id
    public static String token;
    public static String hx_id;
    public static String hx_pwd;
    public static String userInfo;//个人页的缓存数据
    public static String contentListJson;//广场页的缓存数据
    public static String backgroundImg;//背景url
    public static String bindPhone;//临时存储绑定的手机号
    public static boolean needUpdate = false;
    public static long downloadId;
    public static int tab;//首页跳转的tab
    public static boolean needSetTab;//首页需要进行跳转
    public static boolean needSetMainTab; //需要跳转到海选页
    static SharedPreferences sp;
    static SharedPreferences.Editor editor;

    public static void init(Context c) {
        sp = c.getSharedPreferences(c.getPackageName(),
                Context.MODE_PRIVATE);
        editor = sp.edit();
        editor.apply();
        refresh();
    }

    public static void refresh() {
        token = sp.getString("token", "");
        userId = sp.getInt("userId", -1);
        userInfo = sp.getString("userInfo", "");
        contentListJson = sp.getString("contentListJson", "");
        backgroundImg = sp.getString("background_img", "");
        bindPhone = sp.getString("bindPhone", "");
        needUpdate = sp.getBoolean("need_update", false);
        downloadId = sp.getLong("download_id", 0);
        tab = sp.getInt("tab", 0);
        needSetTab = sp.getBoolean("need_set_tab", false);
        needSetMainTab = sp.getBoolean("need_set_main_tab", false);
    }

    public static void save() {
        editor.putString("token", token);
        editor.putInt("userId", userId);
        editor.putString("userInfo", userInfo);
        editor.putString("hx_id", hx_id);
        editor.putString("hx_pwd", hx_pwd);
        editor.putString("contentListJson", contentListJson);
        editor.putString("background_img", backgroundImg);
        editor.putString("bindPhone", bindPhone);
        editor.putBoolean("need_update", needUpdate);
        editor.putLong("download_id", downloadId);
        editor.putInt("tab", tab);
        editor.putBoolean("need_set_tab", needSetTab);
        editor.putBoolean("need_set_main_tab", needSetMainTab);
        editor.apply();
    }

}
